package com.starblink.library.widget.novice_guide.branddetails

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.view.*
import com.skydoves.balloon.BalloonGuide
import com.starblink.android.basic.ext.dp
import com.starblink.android.basic.ext.margin
import com.starblink.library.widget.novice_guide.control.GuidePopStatusControl
import com.starblink.android.basic.ext.enableCorner
import com.starblink.android.basic.ext.toBitmap2LayoutEnhanced
import com.starblink.android.basic.sensorsdata.room.entity.SkAntEntity
import com.starblink.android.basic.sensorsdata.spm.GTrackerAssistUtils
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.sensorsdata.spm.event.TrackEvent
import com.starblink.android.basic.sensorsdata.spm.impl.SpmTrackHandler
import com.starblink.android.basic.user.UserDataCenter
import com.starblink.android.basic.util.FlowCountDownTimer
import com.starblink.basic.style.R
import com.starblink.library.widget.databinding.AddVBrandDetailsFollowToastBinding
import com.starblink.library.widget.databinding.AddVHomeBrandToastBinding
import com.starblink.library.widget.novice_guide.home.HomeBrandPopViewNoData

object BrandDetailsFollowPopView {
    private val downTimer = FlowCountDownTimer(time = 5)
    private var anchorBitmap: Bitmap? = null

    //埋点只上报一次
    private var show = false

    @SuppressLint("ClickableViewAccessibility")
    fun pushPopWindow(
        anchorView: View,
        parent: ViewGroup,
        dismissCallBack: (() -> Unit)? = null
    ) {
        GuidePopStatusControl.getHollowRectF(anchorView)?.let { rectF ->
            //↓
            val toastLay =
                AddVBrandDetailsFollowToastBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
            toastLay.btm.enableCorner(8.dp)
            toastLay.btm.margin(
                leftMargin = rectF.left.toInt(),
                topMargin = rectF.top.toInt()
            )

            downTimer.start(callBack = {
                anchorBitmap = anchorView.toBitmap2LayoutEnhanced()
                if (anchorBitmap != null) {
                    toastLay.btm.setImageBitmap(anchorBitmap)
                    if (!show) {
                        show = true
                        var balloon = BalloonGuide.Builder(parent.context)
                            .setLayout(toastLay)
                            .setPadding(0)
                            .setOnBalloonDismissListener {
                                dismissCallBack?.invoke()
                            }.setOnBalloonClickListener {
//                            SpmTrackHandler.addSingleTrackData(
//                                SkAntEntity(
//                                    spmCnt = GTrackerAssistUtils.fetchElementValue(
//                                        SpmPageDef.Home080Page_10164,
//                                        SpmElementDef.Element_33069
//                                    ), params = mapOf("function" to "brandCollections")
//                                )
//                            )
                            }
                            .setBackgroundColorResource(R.color.sk_gray900_opacity70)
                            .build()

                        balloon.showFull(parent)
//                    SpmTrackHandler.addSingleTrackData(
//                        SkAntEntity(
//                            event = TrackEvent.elementExposure.value,
//                            spmCnt = GTrackerAssistUtils.fetchElementValue(
//                                SpmPageDef.Home080Page_10164,
//                                SpmElementDef.Element_33069
//                            ), params = mapOf("function" to "brandCollections")
//                        )
//                    )
                    }
                }
            }, onFinish = { })


        }

    }
}

